#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//初始化棋盘
void LnitMineShow(char arr[ROWS][COLS], int rows,int cols,char c)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = c;
		}
	}
}
//打印棋盘
void PrintMineShow(char arr[ROWS][COLS],int row,int col)
{
	int i = 0;
	printf("-------扫雷-------\n");
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}
//布置雷
void SetTnt(char arr[ROWS][COLS], int row, int col)
{
	int count = Easy_Count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
	
}
static int TntCount(char mine[ROWS][COLS],int x, int y)
{
	int i = 0;
	int sum = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		int j = 0;
		for (j = y - 1; j <= y + 1; j++)
		{
			sum += (mine[i][j] - '0');
		}
	}
	return sum;
}
//static int GetMineCount(char mine[ROWS][COLS],int x, int y)
//{
//	return mine[x - 1][y] + 
//		mine[x - 1][y - 1] + 
//		mine[x][y - 1] + 
//		mine[x + 1][y - 1] +
//		mine[x + 1][y] + 
//		mine[x + 1][y + 1] + 
//		mine[x][y + 1] + 
//		mine[x - 1][y + 1] - 8*'0';
//}
//排查雷
void InvestTnt(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
	int count = 0;
	int x = 0;
	int y = 0;
	while (count <row*col - Easy_Count)
	{
		printf("请输入要排查的坐标：");
		scanf("%d%d", &x, &y);
		//判断坐标是否有效
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾，踩到炸弹了，你死了！！！\n");
					PrintMineShow(mine, ROW, COL);
					break;
				}
				else
				{
					int sum = TntCount(mine, x, y);
					//int sum = GetMineCount(mine, x, y);
					show[x][y] = sum + '0';
					PrintMineShow(show, ROW, COL);
					count++;
				}
			}
			else
			{
				printf("该坐标已经被排查，请重新输入：\n");
			}
		}
		else
		{
			printf("非法输入，请重新输入：\n");
		}
	}
	if (row * col - Easy_Count == count)
	{
		printf("恭喜您，挑战成功！！！");
		PrintMineShow(mine,ROW,COL);
	}
}
